package kr.co.bettersoft.usedmarketandroid.adapter;

import java.util.ArrayList;

import kr.co.bettersoft.usedmarketandroid.activity.R;
import kr.co.bettersoft.usedmarketandroid.activity.UsedMarketChatRoomListActivity;
import kr.co.bettersoft.usedmarketandroid.common.CommonUtil;
import kr.co.bettersoft.usedmarketandroid.domain.UsedMarketChat;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class UsedMarketChatRoomAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<UsedMarketChat> chatRoomList;
	private UsedMarketChat chatRoom;
	LayoutInflater layoutInflater;
	
	CommonUtil commonUtil;
	UsedMarketChatRoomListActivity usedMarketChatRoomListActivity;
	
	String TAG = "UsedMarketChatRoomAdapter";
	
	View view;
	
	public UsedMarketChatRoomAdapter(Context context, UsedMarketChatRoomListActivity usedMarketChatRoomListActivity, ArrayList<UsedMarketChat> chatRoomList) {
		this.context = context;
		this.chatRoomList = chatRoomList;
		this.usedMarketChatRoomListActivity = usedMarketChatRoomListActivity;
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
		commonUtil = new CommonUtil();
	}

	@Override
	public int getCount() {
		if(chatRoomList!=null){
			return chatRoomList.size();
		}else{
			return 0;
		}
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return chatRoomList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		
		view = convertView;
		
		ViewWrapper wrapper = null;
		// 태그를 이용하여 position 값을 정확한 값을 사용하도록 함. 태그를 저장후 나중에 태그값을 꺼내어 사용
		if(view == null) {
			view = layoutInflater.inflate(R.layout.usedmarket_adapter_chat_room_list, parent, false);
			wrapper = new ViewWrapper(view);
			view.setTag(wrapper);
		} else {
			wrapper = (ViewWrapper)view.getTag();
		}
		
		chatRoom = chatRoomList.get(position);
		
		// New / 새메세지
		TextView chat_room_new_room_mark = (TextView) view.findViewById(R.id.chat_room_new_room_mark);
		TextView chat_room_new_message_mark = (TextView) view.findViewById(R.id.chat_room_new_message_mark);
		// 멤버 섬네일
		ImageView  member_thumbnail_img = (ImageView) view.findViewById(R.id.member_thumbnail_img);
		// 타이틀 
		TextView chat_room_title_txt = (TextView) view.findViewById(R.id.chat_room_title_txt);
		// 마지막 메세지
		TextView chat_room_last_message_txt = (TextView) view.findViewById(R.id.chat_room_last_message_txt);
		// 날짜
		TextView date_txt = (TextView) view.findViewById(R.id.date_txt);
		// 안읽은 메시지 개수
		TextView chat_room_message_count_txt = (TextView) view.findViewById(R.id.chat_room_message_count_txt);
		View delete_line_layout =  view.findViewById(R.id.delete_line_layout);
		Button delete_line_btn = (Button) view.findViewById(R.id.delete_line_btn);
		
		member_thumbnail_img.setImageBitmap(((UsedMarketChat)chatRoomList.get(position)).getThumbnail());
		chat_room_title_txt.setText(((UsedMarketChat)chatRoomList.get(position)).getSubject());
		chat_room_last_message_txt.setText(((UsedMarketChat)chatRoomList.get(position)).getContent());
		chat_room_message_count_txt.setText(((UsedMarketChat)chatRoomList.get(position)).getMessageCount());
		date_txt.setText(((UsedMarketChat)chatRoomList.get(position)).getRegisterDate());

		// 새 채팅방 / 안읽은 메시지
		if(((UsedMarketChat)chatRoomList.get(position)).getReadYn().length()<1){			// 새로운 채팅방
			chat_room_new_message_mark.setVisibility(View.GONE);
			chat_room_new_room_mark.setVisibility(View.VISIBLE);
		}else if(((UsedMarketChat)chatRoomList.get(position)).getReadYn().equals("Y")){	// 모두 읽음. 모두 숨김
			chat_room_new_message_mark.setVisibility(View.GONE);
			chat_room_new_room_mark.setVisibility(View.GONE);
		}else if(((UsedMarketChat)chatRoomList.get(position)).getReadYn().equals("N")){	// 안읽음 메시지
			chat_room_new_message_mark.setVisibility(View.VISIBLE);
			chat_room_new_room_mark.setVisibility(View.GONE);
		}

		// 삭제 버튼
		delete_line_btn.setTag(position);
		if(((UsedMarketChat)chatRoomList.get(position)).isShowCancelBtnYn()){
			delete_line_layout.setVisibility(View.VISIBLE);
		}else{
			delete_line_layout.setVisibility(View.GONE);
		}
		delete_line_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				usedMarketChatRoomListActivity.deletePosition = Integer.parseInt(v.getTag()+"");
				usedMarketChatRoomListActivity.confirmAndDelete();
			}
		});
		
		return view;
	}
	
	class ViewWrapper {
		View base;
		ViewWrapper(View base) {
			this.base = base;
		}
	}

}